package net.tngou.c4j;

import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.ml.Model;
import net.tngou.crawler.pojo.Data;
import net.tngou.crawler.util.StringUtil;

/**
 * 整理数据
 * @author tngou@tngou.net
 *
 */
public class ArrangeData {

	public static JSONObject getJSON(List<Data> list) {
		JSONObject[] objects = new JSONObject[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objects[i]=JSON.parseObject(list.get(i).getValue());		
		}
		return merge(objects);
	}
	
	public static JSONObject merge(JSONObject ...objects) {
		JSONObject json = new JSONObject();
		for (JSONObject object : objects) {
			Set<String> keys = object.keySet();
			for (String key : keys) {
				String src = json.getString(key);
				String des = object.getString(key);
				if(StringUtil.isEmpty(src))
				{
					json.put(key, des);
				}else
				{
					Model m_src = new Model(key, src);
					Model m_des = new Model(key, des);
					if(!m_src.equals(m_des))
					{
						if(m_src.contrast(m_des)<50)
						{
							json.put(key, src+des);
						}
					}
					
				}
			}
		}
		return json;
		
	}
}
